<?php
/**
 * 用户资金类
 * Created by PhpStorm.
 * User: 王海东
 * Date: 2018/12/17
 * Time: 14:00
 */

namespace app\common\model;
use think\Db;

class Capital
{
    private const TAB_NAME = 'capital';

    /**
     * 新增资金账户
     * @author 王海东
     * @date
     * @param int $user_id  用户id
     * @return bool
     * @throws \Exception
     */
    public function add(int $user_id)
    : bool {
        try {
            $id = Db::name(self::TAB_NAME)
                    ->insert([
                        "user_id"           => $user_id,
                        "create_time"       => date("Y-m-d H:i:s"),
                    ], false, true);
        } catch (\Exception $e) {
            throw new \Exception("新增账户信息失败");
        }

        return $id ? true : false;
    }


    /**
     * 修改用户余额
     * @author 王海东
     * @date
     * @param int   $user_id    用户id
     * @param float $balance    要修改的余额
     * @return bool|int|true
     * @throws \Exception
     */
    public function setBalance(int $user_id,float $balance)
    :int {

        try {
            $res = Db::name(self::TAB_NAME)
                     ->where('user_id',$user_id)
                     ->setInc('balance',$balance);
        } catch (\Exception $e) {
            throw new \Exception($e->getMessage());
        }

        return $res;
    }
}